package com.inhim.pjhim.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.inhim.pjhim.R;
import com.inhim.pjhim.activity.ProjectListActivity;
import com.inhim.pjhim.activity.ProjectRadioListActivity;
import com.inhim.pjhim.activity.TypeListActivity;
import com.inhim.pjhim.adapter.TypeListAdapter;
import com.inhim.pjhim.entity.ReaderTypeList;
import com.inhim.pjhim.http.MyOkHttpClient;
import com.inhim.pjhim.http.Urls;
import com.inhim.pjhim.view.TransparentProgressDialog;

import org.yczbj.ycrefreshviewlib.view.YCRefreshView;

import java.io.IOException;
import java.util.HashMap;

import okhttp3.Request;

public class TypeListFragment extends Fragment {
    private YCRefreshView ycRefreshView;
    private Gson gson;
    private TypeListAdapter mAdapter;
    private int mPageNum=1;
    private Boolean refresh=true;
    private int totalPage;
    private Context mContext;
    private String TAG;
    private ReaderTypeList.List ReaderType;
    private String mType;
    private TransparentProgressDialog mProgressDialog;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_type_list, container, false);
        Bundle bundle=getArguments();
        mType= (String) bundle.getSerializable("Type");
        TAG= (String) bundle.getSerializable("TAG");
        ReaderType= (ReaderTypeList.List) bundle.getSerializable("ReaderType");
        gson=new Gson();
        mContext=getActivity();
        ycRefreshView=view.findViewById(R.id.ycRefreshView);
        initAdapter();
        getReaderTypeList();
        return view;
    }

    private void initAdapter() {
        mAdapter = new TypeListAdapter(getActivity(),ReaderType,mType);
        ycRefreshView.setLayoutManager(new LinearLayoutManager(mContext));
        ycRefreshView.setEmptyView(R.layout.empty_view_layout);
        ycRefreshView.setAdapter(mAdapter);
        //设置上拉加载更多时布局，以及监听事件
        mAdapter.setMore(R.layout.view_more, () -> {
            //可以做请求下一页操作
            mPageNum++;
            refresh=false;
            getReaderTypeList();
        });
        ycRefreshView.setRefreshListener(() -> {
            mPageNum= 1;
            refresh=true;
            getReaderTypeList();
        });
        mAdapter.setOnItemClickListener(position -> {
            ReaderTypeList.List reader=mAdapter.getAllData().get(position);
            Intent intent;
            if(reader.getType()==2){
                intent=new Intent(mContext, ProjectListActivity.class);
            }else if(reader.getType()==3){
                intent=new Intent(mContext, ProjectRadioListActivity.class);
            }else{
                intent=new Intent(mContext, ProjectListActivity.class);
            }
            intent.putExtra("TAG",TAG);
            intent.putExtra("ReaderTypeList",reader);
            startActivity(intent);
        });
    }

    private void getReaderTypeList(){
        if(ReaderType!=null){
            mAdapter.setReaderType(ReaderType);
        }
        //showLoading("加载中");
        HashMap postMap=new HashMap();
        if(ReaderType==null){
            return;
        }
        String url= Urls.getReaderTypeList(mPageNum, 10, String.valueOf(ReaderType.getType()),ReaderType.getReaderTypeId());
        MyOkHttpClient.getInstance().asyncJsonPostNoToken(url,postMap, new MyOkHttpClient.HttpCallBack() {
            @Override
            public void onError(Request request, IOException e) {
                //hideLoading();
            }

            @Override
            public void onSuccess(Request request, String result) {
                //hideLoading();
                try{
                    ReaderTypeList readerLists = gson.fromJson(result, ReaderTypeList.class);
                    if(readerLists.getCode()==0){
                        totalPage=readerLists.getPage().getTotalPage();
                        if(ReaderType!=null){
                            if(refresh){
                                mAdapter.clear();
                            }
                            for (int i = 0; i < readerLists.getPage().getList().size(); i++) {
                                mAdapter.add(readerLists.getPage().getList().get(i));
                            }
                            if(mPageNum>=totalPage){
                                mAdapter.pauseMore();
                            }
                            mAdapter.notifyDataSetChanged();
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
    }

    public void showLoading(String text) {
        if (mProgressDialog == null) {
            mProgressDialog = new TransparentProgressDialog(mContext,text);
            mProgressDialog.getWindow().setDimAmount(0f);
            //mProgressDialog.setCancelable(false);
            mProgressDialog.setCanceledOnTouchOutside(false);
        }
        try{
            mProgressDialog.show();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void hideLoading() {
        try {
            if (mProgressDialog != null && mProgressDialog.isShowing()) {
                try{
                    mProgressDialog.dismiss();
                    mProgressDialog=null;
                }catch (Exception e){
                    e.printStackTrace();
                }
                mProgressDialog=null;
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
